import pymysql
from dbutils.pooled_db import PooledDB

POOL = PooledDB(
    creator=pymysql,    # 使用链接数据库模块
    maxconnections=6,   # 连接池允许的最大连接数
    mincached=2,        # 初始化的链接数
    host='127.0.0.1',
    blocking=True,      # 没有可用链接则阻塞等待
    ping=0,             # 拿到链接后,ping MySQL服务是否可用. 0:不ping
    port=3306,
    user='root',
    password='123456',
    database='my_blog',
    charset='utf8'
)

def dbpool(func):
    def inner():
        pass
    return inner


def fetchall(sql, *args):
    '''获取所有数据'''
    conn = POOL.connection()
    cursor = conn.cursor()
    cursor.execute(sql, args)
    result = cursor.fetchall()
    cursor.close()
    conn.close()
    return result

def fetchone(sql, *args):
    ''' 获取单条数据 '''
    conn = POOL.connection()
    cursor = conn.cursor()
    cursor.execute(sql, args)
    result = cursor.fetchone()
    cursor.close()
    conn.close()
    return result